package library.common.util;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import library.common.R;
import library.common.view.dialog.CommonDialogFragment;
import library.common.view.dialog.base.IViewBinder;

/**
 * @author Created by qiang.hou on 2018/10/11.
 * @version 1.0
 */
public class CommonDialogUtils {
    /**
     * default loading dialog
     *
     * @param context Context
     * @return Dialog
     */
    public static Dialog getProgressDialog(Context context) {
        // 默认触摸不消失，返回键消失
        return getProgressDialog(context, null, true, false);
    }

    /**
     * default loading dialog
     *
     * @param context     Context
     * @param loadingText loading text
     * @return Dialog
     */
    public static Dialog getProgressDialog(Context context, String loadingText) {
        // 默认触摸不消失，返回键消失
        return getProgressDialog(context, loadingText, true, false);
    }

    /**
     * @param context     Context
     * @param text        message
     * @param cancelable  cancelable
     * @param cancelTouch setCanceledOnTouchOutside
     * @return Dialog
     */
    public static Dialog getProgressDialog(Context context, @Nullable String text, boolean cancelable, boolean cancelTouch) {
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.layout_progress_dialog);
        if (text != null) {
            TextView tv = progressDialog.findViewById(R.id.progress_tv);
            tv.setText(text);
            tv.setVisibility(View.VISIBLE);
        }
        progressDialog.setCancelable(cancelable);
        progressDialog.setCanceledOnTouchOutside(cancelTouch);
        Window window = progressDialog.getWindow();
        if (window != null)
            window.setBackgroundDrawableResource(android.R.color.transparent);
        return progressDialog;
    }

    /**
     * alert dialog with DialogFragment
     *
     * @param fragmentManager android.support.v4.app.FragmentManager
     * @param message         alert message
     * @param callback        callback for ok or cancel result
     */
    public static void showAlertDialog(
            @NonNull FragmentManager fragmentManager, final String message, final IOnAlertCallback callback) {

        Map<String, Integer> textViewBinder = new HashMap<>();
        textViewBinder.put(message, R.id.text_dialog_message);

        new CommonDialogFragment.Builder()
                .layoutId(R.layout.layout_alert_dialog)
                .setTextString(textViewBinder)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int i = v.getId();
                        if (i == R.id.button_cancel && null != callback) {
                            callback.onCancel();
                        } else if (i == R.id.button_ok && null != callback) {
                            callback.onOk();
                        }
                    }
                }, R.id.button_ok, R.id.button_cancel)
                .createDialogFragment().show(fragmentManager);

    }

    /**
     * alert dialog with DialogFragment
     *
     * @param fragmentManager android.support.v4.app.FragmentManager
     * @param message         alert message
     * @param callback        callback for ok or cancel result
     */
    public static void showRetryDialog(
            @NonNull FragmentManager fragmentManager, final String message, final IOnAlertCallback callback) {

        new CommonDialogFragment.Builder()
                .layoutId(R.layout.layout_alert_dialog)
                .bindView(new IViewBinder() {
                    @Override
                    public void bindView(View view) {
                        ((TextView) view.findViewById(R.id.text_dialog_message)).setText(message);
                        ((TextView) view.findViewById(R.id.button_ok)).setText(R.string.text_retry);
                        (view.findViewById(R.id.button_ok)).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (null != callback) {
                                    callback.onOk();
                                }
                            }
                        });

                        (view.findViewById(R.id.button_cancel)).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (null != callback) {
                                    callback.onCancel();
                                }
                            }
                        });
                    }
                })
                .createDialogFragment().show(fragmentManager);

    }

    public static void dismissDialog(Dialog dialog) {
        if (dialog != null) dialog.dismiss();
    }

    public interface IOnAlertCallback {
        void onCancel();

        void onOk();
    }
}
